package com.xuecheng.manage_course.web;

import com.xuecheng.api.course.CourseControllerApi;
import com.xuecheng.framework.domain.cms.response.CoursePreviewResult;
import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.CourseMarket;
import com.xuecheng.framework.domain.course.CourseView;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.ext.CategoryNode;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.request.CourseListRequest;
import com.xuecheng.framework.domain.system.SysDictionary;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: gongfy
 * @create: 2020/7/23 10:52
 * @Description:
 */
@RestController
@RequestMapping("/course")
public class CourseController implements CourseControllerApi {

    @Autowired
    private CourseService courseService;

    /**
    * @Author: gongfy
    * @Param:
    * @Return:
    * @Date: 2020/7/23 15:19
    * @Description: 课程计划查询
    **/
    @PreAuthorize("hasAuthority('course_find_list')") // 在方法上添加授权注解：用来鉴别当前登录用户所拥有的角色是否有xxx权限访问该接口。
    @Override
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachPlanList(@PathVariable(name = "courseId") String courseId) {
        TeachplanNode teachplanNode = courseService.selectTeachNodeList(courseId);
//        if(teachplanNode != null){
//            throw new RuntimeException("teachplanNode,不为空");
//        }
        return teachplanNode;
    }
    /**
    * @Author: gongfy
    * @Param:
    * @Return:
    * @Date: 2020/7/25 16:09
    * @Description: 新增课程计划
    **/
    @PreAuthorize("hasAuthority('course_teachplan_add')")
    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachPlan(@RequestBody Teachplan teachplan) {

        return courseService.addTeachPlan(teachplan);
    }

    /**
    * @Author: gongfy
    * @Param:  page页数   size每页显示条数  courseListRequest查询条件
    * @Return:  QueryResponseResult 存放响应列表list和总记录数total
    * @Date: 2020/7/23 15:18 com.boss.payaccredit.controller.PayCommonWebController
    * @Description:  课程查询分页 PageHelper
    */
    @PreAuthorize("hasAuthority('course_find_list')")
    @Override
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult findCourseList(@PathVariable(name = "page") int page,
                                              @PathVariable(name = "size") int size,
                                               CourseListRequest courseListRequest) {
        QueryResponseResult courseList = courseService.findCourseList(page, size, courseListRequest);
        return courseList;
    }

    //新增课程
    @Override
    @PostMapping("/coursebase/add")
    public ResponseResult addCourse(@RequestBody CourseBase courseBase) {
        return courseService.addCourse(courseBase);
    }

    //查询课程等级、学习模式
    @Override
    @GetMapping("/dictionary/get/{dType}")
    public SysDictionary findCourseGrade(@PathVariable(name = "dType") String dType) {
        return courseService.findCourseGrade(dType);
    }

    //课程分类查询
    @Override
    @GetMapping("/category/list")
    public CategoryNode findCategoryList() {

        return courseService.findCategoryList();
    }

    //根据id查询课程
    @Override
    @GetMapping("/coursebase/{courseId}")
    public CourseBase findCourseBaseById(@PathVariable(name = "courseId") String courseId) {
        return courseService.findCourseBaseById(courseId);
    }

    //课程修改  todo 没有通的原因是前端把数据 拼在路径上了。。。
    @Override
    @PostMapping("/coursebase/modify")
    public ResponseResult updateCoursebase(@RequestBody CourseBase courseBase) {
        return courseService.updateCoursebase(courseBase);
    }
    // 获取课程营销信息  todo 待测
    @Override
    @GetMapping("/course/coursemarker/{id}")
    public CourseMarket getCourseMarketById(@PathVariable(name = "id") String id) {
        return courseService.getCourseMarketById(id);
    }
    //修改课程营销信息  todo 待测
    @Override
    @PostMapping("/course/coursemarker/modify")
    public ResponseResult updateCourseMarket(@RequestBody CourseMarket courseMarket) {
        return courseService.updateCourseMarket(courseMarket);
    }

    //课程视图查询
    @Override
    @GetMapping("/courseview/{id}")
    public CourseView findCourseView(@PathVariable(name = "id") String id) {
        return courseService.findCourseView(id);
    }

    //课程预览
    @Override
    @PostMapping("/preview/{courseId}")
    public CoursePreviewResult preview(@PathVariable(name = "courseId") String courseId) {
        return courseService.preview(courseId);
    }


}
